package cn.juforg.gravitysnake;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class Garden extends View {
	
	protected static int gridSize;
	protected static int maxX;
	protected static int maxY;
	
	private static int xOffset;
	private static int yOffset;
	
	private Bitmap[] pieceArray;					//存放图像的数组
	
	private int[][] gardenGrid;						//二维数组 存放花园的所有图片的索引
	
	private final Paint gardenPaint = new Paint();	//画笔对象 
	static final String TAG = "AST"; 
	
	//构造函数
	public Garden(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.GardenView);
		gridSize = ta.getInt(R.styleable.GardenView_gridSize, 32);
		
		ta.recycle();
	}
	public Garden(Context context, AttributeSet attrs) {
		super(context, attrs);

		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.GardenView);
		gardenPaint.setColor(Color.LTGRAY);
		gridSize = a.getInt(R.styleable.GardenView_gridSize, 32);

		a.recycle();
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		Log.i(TAG, "onSizeChanged:"+"w:"+w+"h:"+h);
		//屏幕坐标
		maxX = (int) Math.floor(w / gridSize);
		maxY = (int) Math.floor(h / gridSize);
		//偏移量
		xOffset = (w - maxX * gridSize)/2;
		yOffset = (h - maxY * gridSize)/2;
		
		//初始化数组
		gardenGrid = new int[maxX][maxY];
		initGardenGrid();
	}
	
	//重设图像片数组
	public void resetPiece(int pieceCount){
		pieceArray = new Bitmap[pieceCount];
	}
	
	//初始化花园数组
	public void initGardenGrid(){
		for (int i = 0; i < maxX; i++) {
			for (int j = 0; j < maxY; j++) {
				setGrid(0,i,j);
			}
		}
	}
	
	//设置某个坐标的图片索引
	public void setGrid(int pieceIndex, int x , int y){
		if (null != gardenGrid && gardenGrid.length > 0) {
			gardenGrid[x][y] = pieceIndex;
		}else {
			getGardenGrid();
		}
		
	}
	//设置bitmap
	public void loadPiece(int key, Drawable piece){
		Bitmap bitmap = Bitmap.createBitmap(gridSize, gridSize, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		piece.setBounds(0, 0, gridSize, gridSize);
		piece.draw(canvas);
		pieceArray[key] = bitmap;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Log.i(TAG, "Garden onDraw");
		for (int x = 0; x < maxX; x++) {
			for (int y = 0; y < maxY; y++) {
//				Log.i(TAG, "gardenGrid["+x+"]["+y+"]:"+gardenGrid[x][y]);
				if (gardenGrid[x][y]>0) {//索引不为空
					canvas.drawBitmap(pieceArray[gardenGrid[x][y]], xOffset + x * gridSize, yOffset + y *gridSize, gardenPaint);
				}
			}
		}
	}
	
	public void getGardenGrid() {
		Log.i(TAG, "getGardenGrid:"+"w:"+getWidth()+"h:" + getHeight());
		//屏幕坐标
		maxX = (int) Math.floor(getWidth() / gridSize);
		maxY = (int) Math.floor(getHeight() / gridSize);
		//偏移量
		xOffset = (getWidth() - maxX * gridSize)/2;
		yOffset = (getHeight() - maxY * gridSize)/2;
		
		//初始化数组
		gardenGrid = new int[maxX][maxY];
		initGardenGrid();
	}
	
}
